if(files.cwd().endsWith("脚本")){
  let _require = require;
  require = function(path){
    if(path.startsWith("./")){
      path = path.replace("./","");
    }
    path = files.cwd() + "/gdt_hb/" + path;
    return _require(path);
  }
}


auto.waitFor()
var utils = require("./utils.js");
var storage = storages.create("xiangxiangyuedu");
var time = random(2000, 3000)

var checkDialogThread = threads.start(function () {
  while (true) {
    var dismiss = id("tt_insert_dislike_icon_img").findOne(5000);
    if (dismiss) {
      log("检测到弹窗,关闭.")
      sleep(1000)
      click(dismiss.bounds().centerX(),dismiss.bounds().centerY())
      sleep(1000);
    }
    var appDiaDismiss =  className('android.view.ViewGroup').clickable(true).depth(8).drawingOrder(1).findOne(1000)
    if(appDiaDismiss){
      log("检测到弹窗,关闭.")
      sleep(1000)
      click(appDiaDismiss.bounds().centerX(),appDiaDismiss.bounds().centerY())
      sleep(1000)
    }
    sleep(5000);
    log("没有检测到弹框.")
  }
});


function launchAppEnhance(app_name) {
  let thread = threads.start(function () {
    toastLog("检查是否有打开权限!");
    while (true) {
      var allow = text("允许").findOne(5000);
      if (allow) {
        sleep(1000)
        toastLog("发现允许授权!");
        allow.click();
        sleep(1000);
        thread.interrupt();
        break;
      }
    }
  });
  sleep(1000);
  var result = launchApp(app_name);
  sleep(3000);
  thread.interrupt();
  return result;
}

function task(){
  var gx1 = textContains("养鱼生财").findOne(30000)
  if(gx1){
      log("开始 鱼生财 任务")
      var finalActivity = currentActivity();
      click(gx1.bounds().centerX(),gx1.bounds().centerY());
      sleep(5000);
      utils.waitAdvert(finalActivity);
      sleep(3000)
      kapai();
      sleep(3000)
      log("鱼生财 任务完成")
  }else{
    toastLog('未找到养鱼生财 未找到' );
    return ;
  }
  utils.updateAppProgress(8);
  var is_jiasu = false;
  log("开始加速 任务")
  //加速
  for(var i = 0;i<8;i++){
    var 加速 = className("android.widget.TextView").text("加速").findOne(1000);
    if(加速){
      log("第 [" + i + "]次加速.")
      var finalActivity = currentActivity();
      加速.parent().click();
      sleep(5000);
      utils.waitAdvert(finalActivity);
      is_jiasu = true;
    }
    sleep(3000)
  }
  utils.updateAppProgress(60);
  if(is_jiasu){
    log("加速完成，等待65s")
    sleep(65000)
  }
  
  
  //九鱼聚财
  var jiuyu =  className('android.view.ViewGroup').clickable(true).depth(22).drawingOrder(1).findOne(1000)
  if(jiuyu){
    log("开始九鱼聚财 任务")
    var finalActivity = currentActivity();
    click(jiuyu.bounds().centerX(),jiuyu.bounds().centerY())
    sleep(5000);
    utils.waitAdvert(finalActivity);
    sleep(3000)
    kapai();
    log("九鱼聚财 任务完成")
    utils.updateAppProgress(90);
  }else{
    toastLog('未找到九鱼聚财任务');
    return ;
  }
  //奖励
  click(482,782,582,820)
  log("奖励已领取.")
  sleep(2000)
  log("任务完成")
}


  function kapai(){
    var kapais = className("android.view.ViewGroup").clickable(true).depth(17).find();
    kapais.forEach(kp => {
      var finalActivity = currentActivity();
      kp.click();
      log("卡牌 翻牌..")
      utils.waitAdvert(finalActivity);
      sleep(3000)
      var lingqu = text("领取").findOnce();
      if(lingqu){
        lingqu.parent().click();
      }else{
        toastLog('翻牌领取未找到');
        return ;
      }
      
    });
  }

function main() {
    let open_app = launchAppEnhance("天天有鱼国际版");
    if(open_app) {
      task() //正常情况 养鱼生财 -> 加速 -> 九鱼聚财 -> 领取奖励
      toastLog("兼容国际版AD情况,在执行一次")
      task() //AD 情况 九鱼聚财 ->养鱼生财  -> 加速 -> 领取奖励
      
    }
    else {
      toastLog("天天有鱼打开失败，请确认是否安装并授权打开!");
    }
  }
  
try {
    main();
    utils.updateAppProgress(100);
  } catch (error) {
    toastLog(error);
    
  }
  checkDialogThread.interrupt();
  storage.put("script_running_status", "end");